﻿Imports System.Data.OleDb
Public Class EditProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            Dim sql As String = "SELECT * FROM UserProfile WHERE DesignerID=@f6"
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("DesignerConnectionString").ConnectionString)
            conn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
            cmd.Parameters.AddWithValue("@f6", User.Identity.Name)

            Dim dr = cmd.ExecuteReader()
            dr.Read()
            tb_firstName.Text = dr("FirstName")
            tb_lastName.Text = dr("LastName")

            Dim UserDOB As DateTime = dr("DateOfBirth")

            ddl_year.SelectedValue = UserDOB.Year
            ddl_month.SelectedValue = UserDOB.Month
            ddl_day.SelectedValue = UserDOB.Day
        End If

    End Sub

    Protected Sub btn_submit_Click(sender As Object, e As EventArgs) Handles btn_submit.Click

        Dim newFileName As String
        newFileName = Guid.NewGuid().ToString & getSuffix(fup_picture.FileName)

        If fup_picture.HasFile Then
            fup_picture.SaveAs(Server.MapPath("Pictures") & "/" & newFileName)
        End If

        Dim USerDOB As DateTime = New DateTime(CInt(ddl_year.SelectedValue), CInt(ddl_month.SelectedValue), CInt(ddl_day.SelectedValue))

        Dim sql As String = "UPDATE UserProfile SET FirstName=@f1, LastName=@f2, Gender=@f3, DateOfBirth=@f4, ProfilePicture=@f5 WHERE DesignerID=@f6"

        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("DesignerConnectionString").ConnectionString)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", tb_firstName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_lastName.Text)
        cmd.Parameters.AddWithValue("@f3", ddl_gender.SelectedValue)
        cmd.Parameters.AddWithValue("@f4", USerDOB.Date)
        cmd.Parameters.AddWithValue("@f5", "Pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f6", User.Identity.Name)

       
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()


    End Sub

    Function getSuffix(ByRef fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function

End Class